package com.yitianyigexiangfa.main;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class FileReadTool2 {

    private static List<Double> oneList;

    private static String sep;

    // 文件夹名称
    private static String folderName;
    // 标签行
    private static String flag;
    // 要读取的行数
    private static int count;
    // 输出文件文件名后缀
    private static String resultName;

    static {
        oneList = new ArrayList<Double>();

        sep = File.separator;
    }

    public static void main(String[] args) {
        String outName = null;
        int paramslength = args.length;
        if (paramslength == 4) {
            folderName = args[0];
            flag = args[1];
            count = Integer.parseInt(args[2]);
            resultName = args[3];
        } else {
            System.out.println("参数1： 文件夹名称， 参数2：行标志字符串， 参数3：读取文件行数， 参数4：输出文件文件名后缀");
            return;
        }
        System.out.println(
                "以 \"" + flag + "\"为行标志，读取：" + count + " 行。 处理： " + folderName + " 文件夹及其所有子文件夹下的文件， 输出文件的文件名后缀为：" + resultName);
        readAFolder(folderName, flag, count);
    }

    public static void readAFolder(String folderName, String flag, int count) {
        // ------------ 读取文件夹下的所有文件的名称 ------------
        String pwd = System.getProperty("user.dir");
        String path = pwd + sep + folderName;
        File folder = new File(path);
        File[] files = folder.listFiles();
        readAPath(files);
    }

    public static void readAPath(File[] files) {
        String srcName = null;
        // 读取一个路径，判断这个路径是文件还是文件夹
        for (File file : files) {
            if (file.isDirectory()) {
                System.out.println("正在读取文件夹：" + file.getAbsolutePath());
                readAPath(file.listFiles());
            } else if (file.isFile()) {
                System.out.println("正在读取文件： " + file.getAbsolutePath());
                srcName = file.getAbsolutePath();
                readAFile(srcName);
            }
        }
    }

    public static void readAFile(String srcName) {
        String outName = srcName + "-" + resultName + ".txt";
        // ------------ 读取文件中的指定行 -------------------
        File src = new File(srcName);
        BufferedReader reader = null;
        File dir = new File(outName);
        FileOutputStream fileOutputStream = null;
        try {
            String lineTmp = null;
            int counter = 0;
            reader = new BufferedReader(new FileReader(src));
            lineTmp = reader.readLine();
            while ((lineTmp = reader.readLine()) != null) {
                if (lineTmp.indexOf(flag) != -1) {
                    counter++;
                    fileOutputStream = new FileOutputStream(dir);
                }
                if (counter > 0) {
                    if (counter <= count + 1) {
                        if (lineTmp.length() > 3) {
                            String[] nums = lineTmp.trim().split("\\s+");
                            if (nums[0].equals("Entropy")){
                                oneList.add(Double.parseDouble(nums[1]));
                            }
                        }
                        counter++;
                    } else {
                        System.out.println("文件读取结束，读取结果输出到：" + outName);
                        break;
                    }
                }
            }
        } catch (FileNotFoundException e) {
            System.out.println("文件不存在于该路径下");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("文件读取过程中出现错误");
        }
        try {
            if (fileOutputStream != null) {
                writeLine(fileOutputStream, oneList);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void writeLine(FileOutputStream fileOutputStream, List<Double> list) throws IOException {
        for (int i = 0; i < list.size(); i++) {
            String doubleValue = String.format("%.3f", list.get(i));
            fileOutputStream.write(doubleValue.getBytes());
            fileOutputStream.write("\t".getBytes());
            if (i >= 9) {
                fileOutputStream.write(" ".getBytes());
            }
        }
        fileOutputStream.write("\r\n".getBytes());
        list.clear();
    }
}